﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HCQ2_IBLL;
using HCQ2_Model.SelectModel;
using HCQ2_Model;
using HCQ2_Model.DocModel;
using System.Diagnostics;
using System.Data;
using HCQ2_Common;
using HCQ2_Model.DocModel.APIModel;

namespace HCQ2_BLL
{
    public partial class T_VRCustomPassWordBLL: IT_VRCustomPassWordBLL
    {
        private List<InportWordModel> cpInfo = new List<InportWordModel>();
        /// <summary>
        ///  获取关卡（课件id）
        /// </summary>
        /// <param name="file_id"></param>
        /// <returns></returns>
        public List<SelectModel> GetPassData(int file_id)
        {
            return DBSession.IT_VRCustomPassWordDAL.GetPassData(file_id);
        }
        /// <summary>
        ///  获取关卡单词列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<HeadWordModel> GetTableData(HeadWordParam model, out int total)
        {
            return DBSession.IT_VRCustomPassWordDAL.GetTableData(model,out total);
        }
        /// <summary>
        ///  导入关卡单词
        /// </summary>
        /// <param name="tempPath">execl路径</param>
        /// <param name="file_id">课件id</param>
        /// <param name="file_name">课件名称</param>
        /// <returns></returns>
        public List<string> importWord(string tempPath, int file_id, string file_name)
        {
            //获取导入文件里面的数据
            Stopwatch sw = new Stopwatch();
            sw.Start();
            List<string> valid = new List<string>();
            DataTable excelData = NpoiHelper.ExcelToDataTable(tempPath, true, 0);
            if (excelData == null)
                valid.Add("无法获取到excel文件中数据");
            sw.Stop();
            valid = checkRequired(excelData, file_id, file_name);
            if (valid.Count > 0)
                return valid;
            List<T_VRCustomPassWord> list = toImportModelTable(excelData, file_id, file_name);
            sw.Restart();
            list.ForEach(s => { DBSession.IT_VRCustomPassWordDAL.Add(s); });
            sw.Stop();
            return valid;
        }
        /// <summary>
        /// 验证必填信息是否为空
        /// </summary>
        /// <param name="excelData"></param>
        /// <returns></returns>
        private List<string> checkRequired(DataTable excelData,int file_id,string file_name)
        {
            List<string> valid = new List<string>();
            for (int i = 0; i < excelData.Rows.Count; i++)
            {
                int rowNum = (i + 1 + 1);
                if (excelData.Rows[i]["F1"] == null || string.IsNullOrEmpty(Convert.ToString(excelData.Rows[i]["F1"])))
                    valid.Add(string.Format("第{0}行，关卡代码不能为空", rowNum));
                else if (excelData.Rows[i]["F2"] == null || string.IsNullOrEmpty(Convert.ToString(excelData.Rows[i]["F2"])))
                    valid.Add(string.Format("第{0}行，单词不能为空", rowNum));
                else
                {
                    string temp = Convert.ToString(excelData.Rows[i]["F1"]);
                    T_VRCustomPass pass = DBSession.IT_VRCustomPassDAL.Select(s => s.file_id == file_id && s.cp_code.Equals(temp)).FirstOrDefault();
                    if (pass == null || string.IsNullOrEmpty(pass.cp_code))
                        valid.Add(string.Format("第{0}行，关卡代码【{1}】在后台关卡中不存在、无法进行导入！", rowNum, temp));
                    else
                    {
                        cpInfo.Add(new InportWordModel { cp_id = pass.cp_id, cp_name = pass.cp_name });
                        if (checkWord(Convert.ToString(excelData.Rows[i]["F2"]), file_id, pass.cp_id))
                            valid.Add(string.Format("第{0}行，单词【{1}】已经存在于课件：{2} 之中存在！", rowNum, Convert.ToString(excelData.Rows[i]["F2"]), file_name));
                    }
                } 
            }
            return valid;
        }
        //验证是否存在
        private bool checkWord(string word,int file_id, int cp_id)
        {
            //验证单词是否存在
            int mark = DBSession.IT_VRCustomPassWordDAL.SelectCount(s => s.file_id == file_id && s.cp_id == cp_id && s.word.Equals(word));
            return mark > 0 ? true : false;
        }
        /// <summary>
        ///  数据转换为导入的数据
        /// </summary>
        /// <param name="excelData">原始数据</param>
        /// <param name="file_id"></param>
        /// <param name="file_name"></param>
        /// <param name="cp_id"></param>
        /// <param name="cp_name"></param>
        /// <returns></returns>
        private List<T_VRCustomPassWord> toImportModelTable(DataTable excelData, int file_id,string file_name)
        {
            List<T_VRCustomPassWord> list = new List<T_VRCustomPassWord>();
            T_User user = HCQ2UI_Helper.OperateContext.Current.Usr;
            for (int i = 0; i < excelData.Rows.Count; i++)
            {
                list.Add(new T_VRCustomPassWord
                {
                    cp_id = cpInfo[i].cp_id,
                    cp_name = cpInfo[i].cp_name,
                    file_id = file_id,
                    file_name = file_name,
                    word = Helper.ToString(excelData.Rows[i]["F2"]),
                    gj_symbol = Helper.ToString(excelData.Rows[i]["F3"]),
                    kk_symbol = Helper.ToString(excelData.Rows[i]["F4"]),
                    cp_type = Helper.ToString(excelData.Rows[i]["F5"]),
                    china_mean = Helper.ToString(excelData.Rows[i]["F6"]),
                    create_name = user.user_name,
                    create_date = DateTime.Now,
                    update_name = user.user_name,
                    update_date = DateTime.Now
                });
            }
            return list;
        }
    }
}
